import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_ahj_app/app/project/View/projectListItem.dart';
import 'package:flutter_ahj_app/app/project/ViewModel/projectViewModel.dart';
import 'package:flutter_ahj_app/base/basewidget/view/BaseWidget.dart';
import 'package:flutter_ahj_app/base/utils/JUIKit/JText.dart';
import 'package:flutter_ahj_app/base/utils/mTopBar.dart';
import 'package:flutter_ahj_app/base/values/SQColor.dart';
import 'package:flutter_ahj_app/base/values/dimens.dart';

class projectListView extends BaseStatefulWidget {
  @override
  State<StatefulWidget> initState() {
    // TODO: implement initState
    return projectListViewState();
  }
}

class projectListViewState
    extends BaseState<projectViewModel, projectListView> {
  ScrollController _scrollController;
  bool _isStatus = false; // 项目状态
  bool _isSort = false; // 默认排序

  List _statusList = List();
  List _sortList = List();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _statusList = [
      '全部',
      '项目已保存',
      '项目审批中',
      '项目被驳回',
      '项目已审批',
      '项目审批中',
      '项目已签订',
      '已发布',
      '进行中',
      '已完成'
    ];
    _sortList = [
      '创建时间',
      '最近修改'
    ];
  }

  @override
  Widget initWidget() {
    // TODO: implement initWidget
    return MaterialApp(
        home: Scaffold(
      appBar: MTopBar(
        title: "我跟进的项目",
        centerTitle: true,
      ).create(),
      body: _bodyView(),
    ));
  }

  _bodyView() {
    return Column(
      children: <Widget>[
        Container(
          height: 50,
          child: Row(
            children: <Widget>[
              Expanded(
                flex: 1,
                child: Container(
                  height: Dimens.dimens48,
                  child: RaisedButton(
                    color: SQColor.white,
                    textColor: SQColor.theme_color,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        Text('项目状态'),
                        Icon(Icons.arrow_drop_down),
                      ],
                    ),
                    onPressed: () {
                      setState(() {
                        _isStatus = !_isStatus;
                        _isSort = false;
                      });
                    },
                  ),
                ),
              ),
              Expanded(
                flex: 1,
                child: Container(
                  height: Dimens.dimens48,
                  child: RaisedButton(
                    color: SQColor.white,
                    textColor: SQColor.theme_color,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        Text('默认排序'),
                        Icon(Icons.arrow_drop_down),
                      ],
                    ),
                    onPressed: () {
                      setState(() {
                        _isSort = !_isSort;
                        _isStatus = false;
                      });
                    },
                  ),
                ),
              ),
            ],
          ),
        ),
        Expanded(
          flex: 1,
          child: Stack(
            children: <Widget>[
              ListView.builder(
                shrinkWrap: true,
                physics: AlwaysScrollableScrollPhysics(),
                controller: _scrollController,
                itemCount: 100,
                itemBuilder: (BuildContext context, int index) {
                  return buildModule(context, index);
                },
              ),
              Offstage(
                offstage: !_isStatus,
                child: Container(
                  color: SQColor.white,
                  child: ListView(
                    physics: AlwaysScrollableScrollPhysics(),
                    children: _getStatusItems(),
                  ),
                ),
              ),
              Offstage(
                offstage: !_isSort,
                child: Container(
                  color: SQColor.white,
                  child: ListView(
                    physics: AlwaysScrollableScrollPhysics(),
                    children: _getSortItems(),
                  ),
                ),
              ),
            ],
          ),
        )
      ],
    );
  }

  _fetchData() {}

  Widget buildModule(BuildContext context, int index) {
    return projectListItem();
  }

  _getStatusItems() {
    // 不带分割线的item
    List<Widget> items = new List();
    for (int i = 0; i < _statusList.length; i++) {
      items.add(_getStatusItem(i));
    }
    // 带有分割线的item
    List<Widget> divideItems = ListTile.divideTiles(
      context: context,
      tiles: items,
    ).toList();
    return divideItems;
  }

  _getSortItems() {
    // 不带分割线的item
    List<Widget> items = new List();
    for (int i = 0; i < _sortList.length; i++) {
      items.add(_getSortItem(i));
    }
    // 带有分割线的item
    List<Widget> divideItems = ListTile.divideTiles(
      context: context,
      tiles: items,
    ).toList();
    return divideItems;
  }

  Widget _getSortItem(int index) {
    return Ink(
      child: InkWell(
        onTap: () {
          setState(() {});
        },
        child: Container(
          padding: EdgeInsets.fromLTRB(0, 10, 0, 10),
          child: JText(
            _sortList[index],
            color: Color(0xff333333),
            fontSize: 16,
            textAlign: TextAlign.center,
          ),
        ),
      )
    );
  }

  Widget _getStatusItem(int index) {
    return Ink(
      child: InkWell(
        onTap: () {
          setState(() {});
        },
        child: Container(
          padding: EdgeInsets.fromLTRB(0, 10, 0, 10),
          child: JText(
            _statusList[index],
            color: Color(0xff333333),
            fontSize: 16,
            textAlign: TextAlign.center,
          ),
        ),
      ),
    );
  }
}
